const mysql = require('mysql')
// mysql连接过程封装
function query() {
    const connection = mysql.createConnection({
        host: 'localhost',
        user: 'root',
        port: '3306',
        password: '123vs456',
        database: 'my_app',
    })
    connection.connect();
    connection.query(...arguments)
    connection.end()

}
// 用户登录，查询用户数据并返回
function login(username, password) {
    return new Promise((reslove, reject) => {
        query(`SELECT * FROM users where s_username = '${username}' and s_password = '${password}'`, function (err, res, fields) {
            if (err) {
                data = err.sqlMessage
                return;
            }
            reslove(res)
        })
    })
}
// 获取登录用户信息
function getUserInfo(username) {
    return new Promise((reslove, reject) => {
        query(`SELECT * FROM users where s_username = '${username}'`, function (err, res, fields) {
            if (err) {
                data = err.sqlMessage
                return;
            }
            reslove(res)
        })
    })
}
// 获取登录用户好友信息
function getFriends(username) {
    return new Promise((reslove, reject) => {
        query(`SELECT * FROM friends where s_host_name = '${username}'`, function (err, res, fields) {
            if (err) {
                data = err.sqlMessage
                return;
            }
            reslove(res)
        })
    })
}

module.exports = {
    login,
    getUserInfo,
    getFriends,
}